package com.base.common.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;

public class NetworkUtils {

    public static final int NETWORK_STATE_BREAK = -1;//网络断开
    public static final int NETWORK_STATE_CONNET = 0;//wifi移动都连接
    public static final int NETWORK_STATE_MOBILE = 1;//移动数据连接
    public static final int NETWORK_STATE_WIFI = 2;//wifi


    public static int networkState = -1;//当前网络状态

    public static int getNetworkType(Context context) {

        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (manager != null) {

            int state = 0;//连接状态  0 未连接  1移动数据 2wifi  3 wifi和移动数据

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                //获取所有网络连接的信息
                Network[] networks = manager.getAllNetworks();
                //通过循环将网络信息逐个取出来
                for (int i = 0; i < networks.length; i++) {
                    //获取ConnectivityManager对象对应的NetworkInfo对象
                    NetworkInfo networkInfo = manager.getNetworkInfo(networks[i]);
                    if (networkInfo != null) {
                        if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                            state += 1;
                        } else if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                            state += 2;
                        }
                    }

                }

            } else {
                //获取ConnectivityManager对象对应的NetworkInfo对象
                //获取WIFI连接的信息
                NetworkInfo wifiNetworkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                //获取移动数据连接的信息
                NetworkInfo dataNetworkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

                if (dataNetworkInfo != null && dataNetworkInfo.isConnected()) {
                    state += 1;
                }
                if (wifiNetworkInfo != null && wifiNetworkInfo.isConnected()) {
                    state += 2;
                }

            }


            switch (state) {
                case 0:
                    return NETWORK_STATE_BREAK;
                case 1:
                    return NETWORK_STATE_MOBILE;
                case 2:
                    return NETWORK_STATE_WIFI;
                case 3:
                    return NETWORK_STATE_CONNET;
            }

        }

        return NETWORK_STATE_BREAK;
    }


    public static boolean isWifiConnected(Context context) {
        if (context != null) {
            ConnectivityManager manager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }

        return false;
    }

    public static boolean isConnected(Context context) {
        if (context != null) {
            ConnectivityManager manager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }

        return false;
    }


}
